package com.fick.struts.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.*;
import com.fick.hibernate.model.*;
import com.fick.tools.SystemLogger;
import com.fick.util.General;

public class InfoMessageAction extends DispatchAction {
	// private static Logger log = Logger.getLogger(MessageFeedback.class);

	private static final Log log = LogFactory.getLog(ManagerMessageAction.class);

	public ActionForward listMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SystemLogger.operatelog(request, "查询留言反馈");
		// HttpSession session = request.getSession();
		List list = new ArrayList();
		try {
			// Question question = new Question();
			QuestionDAO questionDao = new QuestionDAO();
			list = questionDao.findAll();
			request.setAttribute("msglist", list);
			log.debug("list.size = " + list.size());
			SystemLogger.operatelog(request, "list.size = " + list.size());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "留言反馈", "查询留言失败");
			return null;
		}
		return mapping.findForward("infoMessage");// listMessage
	}

	public ActionForward delMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		HttpSession session = request.getSession();
		String id = request.getParameter("id");
		try {

			MessageDAO dao = new MessageDAO();
			Message mess = new Message();
			mess = dao.findById(Integer.parseInt(id));
			dao.delete(mess);
			session.setAttribute("message", "删除短信成功");

			return mapping.findForward("infoMessageList");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "删除短信", "删除短信失败");
			return null;
		}
	}

	// 删除多个短信链接
	public ActionForward delLeaderByStr(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SystemLogger.operatelog(request, "删除多个短信");
		HttpSession session = request.getSession();
		String str = request.getParameter("str");
		Message mess = new Message();
		MessageDAO dao = new MessageDAO();
		String[] ids = str.split(",");
		int id = 0;
		try {
			for (int i = 0; i < ids.length; i++) {
				id = Integer.parseInt(ids[i]);
				mess = dao.findById(id);
				dao.delete(mess);
			}
			session.setAttribute("message", "删除短信成功");
			SystemLogger.operatelog(request, "删除短信成功");
			return mapping.findForward("infoMessageList");
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "短信", "删除短信失败");
			return null;
		}
	}

	public ActionForward addMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String addName = "";
		HttpSession session = request.getSession();
		String title = General.convertISOToUTF(request.getParameter("title"));
		String content = General.convertISOToUTF(request.getParameter("content"));
		MessageDAO dao = new MessageDAO();

		try {
			if (session != null) {
				Admin ad = (Admin) session.getAttribute("admin");
				addName = ad.getAdminName();
			} else {
				response.sendRedirect("/login.jsp");
			}
			Message mess = new Message();
			mess.setAddDate(new Date());
			mess.setAddName(addName);
			mess.setContent(content);
			mess.setIfRe("0");
			mess.setReContent("");
			mess.setReDate(new Date());
			mess.setRePerson("");
			mess.setTitle(title);
			dao.save(mess);
			session.setAttribute("message", "添加短信成功");

			SystemLogger.operatelog(request, "添加短信成功!");
			return mapping.findForward("infoMessageList");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "添加短信", "添加短信失败");
			return null;
		}
	}
}
